// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.core.logger;

import java.io.File;
import java.util.Calendar;
import java.util.HashMap;
import zombie.characters.IsoPlayer;
import zombie.debug.DebugLog;

public final class LoggerManager {
    private static boolean s_isInitialized = false;
    private static final HashMap<String, ZLogger> s_loggers = new HashMap();

    public static synchronized ZLogger getLogger(String loggerName) {
        if (!s_loggers.containsKey(loggerName)) {
            createLogger(loggerName, false);
        }

        return (ZLogger)s_loggers.get(loggerName);
    }

    public static synchronized void init() {
        if (!s_isInitialized) {
            DebugLog.General.debugln("Initializing...");
            s_isInitialized = true;
            backupOldLogFiles();
        }
    }

    private static void backupOldLogFiles() {
        try {
            File file0 = new File(getLogsDir());
            String[] string0 = file0.list();
            if (string0 == null || string0.length == 0) {
                return;
            }

            Calendar calendar = getLogFileLastModifiedTime(string0[0]);
            String string1 = "logs_";
            if (calendar.get(5) < 9) {
                string1 = string1 + "0" + calendar.get(5);
            } else {
                string1 = string1 + calendar.get(5);
            }

            if (calendar.get(2) < 9) {
                string1 = string1 + "-0" + (calendar.get(2) + 1);
            } else {
                string1 = string1 + "-" + (calendar.get(2) + 1);
            }

            File file1 = new File(getLogsDir() + File.separator + string1);
            if (!file1.exists()) {
                file1.mkdir();
            }

            for (int _int = 0; _int < string0.length; _int++) {
                string1 = string0[_int];
                File file2 = new File(getLogsDir() + File.separator + string1);
                if (file2.isFile()) {
                    file2.renameTo(new File(file1.getAbsolutePath() + File.separator + file2.getName()));
                    file2.delete();
                }
            }
        } catch (Exception exception) {
            DebugLog.General.error("Exception thrown trying to initialize LoggerManager, trying to copy old log files.");
            DebugLog.General.error("Exception: ");
            DebugLog.General.error(exception);
            exception.printStackTrace();
        }
    }

    private static Calendar getLogFileLastModifiedTime(String string) {
        File file = new File(getLogsDir() + File.separator + string);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(file.lastModified());
        return calendar;
    }

    public static synchronized void createLogger(String loggerName, boolean useConsole) {
        init();
        s_loggers.put(loggerName, new ZLogger(loggerName, useConsole));
    }

    public static String getLogsDir() {
        String string = zombie.ZomboidFileSystem.instance.getCacheDirSub("Logs");
        zombie.ZomboidFileSystem.ensureFolderExists(string);
        File file = new File(string);
        return file.getAbsolutePath();
    }

    public static String getPlayerCoords(IsoPlayer player) {
        return "(" + (int)player.getX() + "," + (int)player.getY() + "," + (int)player.getZ() + ")";
    }
}
